<?php


namespace app\common\service;


use app\common\helper\FormatHelper;
use app\web\constant\CommonConstant;
use think\Db;

/**
 * 优惠券管理服务
 *
 * Class TicketService
 * @package app\common\service
 */
class TicketService {


    /**
     * 通过ID获取优惠券管理数据
     *
     * @param int $ticket_id 优惠券ID
     * @return array|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getTicketById ( int $ticket_id) {

        $ticket_data = self::getTicketByids([$ticket_id]);
        return $ticket_data[0] ?? [];

    }


    /**
     * 通过多个ID获取优惠券管理数据
     *
     * @param array $ticket_ids
     * @return array|\PDOStatement|string|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getTicketByids ( array $ticket_ids) {

        $ticket_data = Db::name('qt_ticket')
            ->whereIn('id', $ticket_ids)
            ->where("is_delete", CommonConstant::IS_DELETE_NO)
            ->select();

        return $ticket_data;
    }


    /**
     * 通过ID获取发送票券数据
     *
     * @param int $send_id
     * @return array|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getTicketSendById(int $send_id) {
        $data = self::getTicketSendByIds([$send_id]);
        return $data[0] ?? [];
    }


    /**
     * 通过多个ID获取发送票券数据
     *
     * @param array $send_ids
     * @return array|\PDOStatement|string|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getTicketSendByIds (array $send_ids) {

        $data = Db::name('qt_ticket_send')
            ->whereIn('id',$send_ids)
            ->where("is_delete",CommonConstant::IS_DELETE_NO)
            ->select();

        return $data;
    }


    /**
     * 使用优惠券
     */
    public static function sendTicket($coupon,$userInfo,$viper_card) {

        foreach ($coupon as $key => $value) {
            $ticket_data = TicketService::getTicketById($value['ticket_id']);
            if (empty($ticket_data)) {
                return FormatHelper::fReturn('没有找到优惠券信息','',FormatHelper::ERROR);
            }
            $insertData['store_id'] = $userInfo['store_id'];
            $insertData['send_num'] = 1;
            $insertData['ticket_id'] = $value['ticket_id'];
            $insertData['phone'] = $viper_card['mobile'];
            $insertData['user_name'] = $viper_card['name'] ?? '';//用户姓名
            $insertData['card_id'] = $viper_card['card_no'] ?? '';//卡号
            $insertData['expiration_time'] = strtotime(date("Y-m-d H:i:s", strtotime("+" . $ticket_data['term'] . " day")));
            $insertData['ticket_code'] = CommonService::uniqidNumber(12,'TK');
            $insertData['creater'] = $insertData['updater'] = $insertData['sender'] = $userInfo['id'];
            $insertData['create_time'] = $insertData['update_time'] = $insertData['send_time'] = time();
            $insertData['is_receive'] = $insertData['is_use'] = $insertData['is_delete'] = CommonConstant::IS_DELETE_NO;

            $insertData['consumption_ewm'] = create_ewm(json_encode(['ticket_code'=>$insertData['ticket_code'],'type'=>1]),'uploads/ticket_send/',$insertData['ticket_code'].'_1.png');
            $insertData['collect_ewm'] = create_ewm(json_encode(['ticket_code'=>$insertData['ticket_code'],'type'=>2]),'uploads/ticket_send/',$insertData['ticket_code'].'_2.png');
            for ($i=1; $i<=$value['send_num']; $i++) {
                Db::name('qt_ticket_send')->insertGetId($insertData);
            }
        }
    }


}